//package com.gitlab.lucky.common.security.security;
//
//import com.gitlab.lucky.common.security.service.JwtUserDetailsService;
//import com.gitlab.lucky.common.security.utils.JwtTokenUtil;
//import org.springframework.beans.factory.annotation.Value;
//import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
//import org.springframework.security.core.context.SecurityContextHolder;
//import org.springframework.security.core.userdetails.UserDetails;
//import org.springframework.security.core.userdetails.UserDetailsService;
//import org.springframework.security.web.authentication.WebAuthenticationDetailsSource;
//import org.springframework.stereotype.Component;
//
//import javax.servlet.*;
//import javax.servlet.annotation.WebFilter;
//import javax.servlet.http.HttpServletRequest;
//import java.io.IOException;
//
///**
// * 请求拦截器
// */
//@Component
//@WebFilter(urlPatterns="/api/*", filterName="restFilter")
//public class JwtAuthorizationTokenFilter implements Filter {
//
//    private final UserDetailsService userDetailsService;
//    private final JwtTokenUtil jwtTokenUtil;
//
//    public JwtAuthorizationTokenFilter(JwtUserDetailsService jwtUserDetailsService, JwtTokenUtil jwtTokenUtil) {
//        this.userDetailsService = jwtUserDetailsService;
//        this.jwtTokenUtil = jwtTokenUtil;
//    }
//    @Override
//    public void init(FilterConfig filterConfig) {
//
//    }
//
//    @Override
//    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
//        final String requestHeader = ((HttpServletRequest) request).getHeader("Authorization");
//        String username = null;
//        String authToken = null;
//        if (requestHeader != null && requestHeader.startsWith("Bearer ")) {
//            authToken = requestHeader.substring(7);
//            try {
//                username = jwtTokenUtil.getUsernameFromToken(authToken);
//            } catch (Exception e) {
//                System.out.println("error");
//            }
//        }
//        if (username != null && SecurityContextHolder.getContext().getAuthentication() == null) {
//            // It is not compelling necessary to load the use details from the database. You could also store the information
//            // in the token and read it from it. It's up to you ;)
//            UserDetails userDetails = this.userDetailsService.loadUserByUsername(username);
//            // For simple validation it is completely sufficient to just check the token integrity. You don't have to call
//            // the database compellingly. Again it's up to you ;)
//            if (jwtTokenUtil.validateToken(authToken, userDetails)) {
//                UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(userDetails, null, userDetails.getAuthorities());
//                authentication.setDetails(new WebAuthenticationDetailsSource().buildDetails(((HttpServletRequest) request)));
//                SecurityContextHolder.getContext().setAuthentication(authentication);
//            }
//        }
//        chain.doFilter(request, response);
//    }
//
//    @Override
//    public void destroy() {
//
//    }
//}
